package com.p2p.front.dal.dao;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.type.JdbcType;

import com.p2p.base.dao.BaseMapper;
import com.p2p.base.model.Money;
import com.p2p.front.dal.model.Finance;
import com.p2p.front.vo.FinanceVO;

public interface FinanceDao extends BaseMapper{
	
	String TABLE_NAME = "p2p_finance";
	
	@Select("select id from " + TABLE_NAME)
	Finance findFineceInfo();
	
	@Insert("insert into " + TABLE_NAME +"(dayAmount,dayEarnings,date) "
			+ "values(#{dayAmount.cent}, #{dayEarnings.cent},#{date})")
	void saveFinanceInfo(@Param("dayAmount")Money dayAmount,
			@Param("dayEarnings")Money dayEarnings, @Param("date")String date);
	
	@Select("select * from " + TABLE_NAME + " order by date desc limit 1")
	@Results(value = { @Result(property = "dayAmount.cent", column = "dayAmount", javaType = long.class, jdbcType = JdbcType.BIGINT) ,
	@Result(property = "dayEarnings.cent", column = "dayEarnings", javaType = long.class, jdbcType = JdbcType.BIGINT),
	@Result(property = "date", column = "date", javaType = String.class, jdbcType = JdbcType.VARCHAR)}) 
	Finance getLatestFinance();
	
	
	@Select("select sum(dayAmount) as totleAmount,sum(dayEarnings) as totleEarnings from " + TABLE_NAME)
	@Results(value = { @Result(property = "totleAmount.cent", column = "totleAmount", javaType = long.class, jdbcType = JdbcType.BIGINT) ,
	@Result(property = "totleEarnings.cent", column = "totleEarnings", javaType = long.class, jdbcType = JdbcType.BIGINT)}) 
	FinanceVO getTotleFinance();
	
	@Delete("delete from "+TABLE_NAME +" where date=#{date}")
	int deleteByDate(@Param("date")String date);
	
}
